import { createRouter, createWebHistory } from 'vue-router'
import HomePage from '@v/HomePage/index.vue'
import {isLogin} from '@u/index'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      meta:{
        needAuth:false
      },
      component: HomePage
    },
    {
      path: '/forum',
      name: 'forum',
      meta:{
        needAuth:false
      },
      component: () => import('@v/Forum/index.vue')
    },
    {
      path: '/choose',
      name: 'choose',
      meta:{
        needAuth:false
      },
      component: () => import('@v/Choose/index.vue')
    },
    {
      path: '/usedCar',
      name: 'usedCar',
      meta:{
        needAuth:false
      },
      component: () => import('@v/UsedCar/index.vue')
    },
    {
      path: '/mine',
      name: 'mine',
      meta:{
        needAuth:true
      },
      component: () => import('@v/Mine/index.vue')
    },
    {
      path: '/login',
      name: 'login',
      meta:{
        needAuth:false
      },
      component: () => import('@v/Login/index.vue')
    },
    {
      path: '/register',
      name: 'register',
      meta:{
        needAuth:false
      },
      component: () => import('@v/Register/index.vue')
    },
    {
      path: '/search',
      name: 'search',
      meta:{
        needAuth:false
      },
      component: () => import('@v/Search/index.vue')
    },
    {
      path: '/details',
      name: 'details',
      meta:{
        needAuth:false
      },
      component: () => import('@v/Details/index.vue')
    },
    {
      path: '/itemLists',
      name: 'itemLists',
      meta:{
        needAuth:false
      },
      component: () => import('@v/ItemLists/index.vue')
    },
    {
      path: '/cart',
      name: 'cart',
      meta:{
        needAuth:true
      },
      component: () => import('@v/Cart/index.vue')
    }
  ]
})
router.beforeEach((to,from)=>{
  //判断
  if(to.meta.needAuth){
    //需要登录
    if(isLogin()){
      return true
    }else{
      return '/login'
    }
  }
})
export default router
